/**
 * ReportGear(2011)
 */
package com.reportgear.designer.parameter.pane;

import java.awt.Component;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;
import javax.swing.SwingUtilities;

import com.reportgear.core.api.Nameable;
import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.designer.gui.component.list.ModifyNameActionListener;
import com.reportgear.designer.gui.controlpane.AbstractNameableCreator;
import com.reportgear.designer.gui.controlpane.JControlPane;
import com.reportgear.designer.gui.controlpane.NameableCreator;
import com.reportgear.report.parameter.model.Parameter;

/**
 * 参数编辑面板
 * 
 * @version 1.0 2011-4-25
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 */
public class ParameterEditPane extends JControlPane<Parameter> {
	private static final long serialVersionUID = -395413860414814510L;

	@SuppressWarnings("serial")
	public ParameterEditPane() {
		setCellRenderer(new DefaultListCellRenderer() {
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
					boolean cellHasFocus) {
				super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

				if (value instanceof Nameable) {
					Nameable nameable = (Nameable) value;
					setText(nameable.getName());
				}

				return this;
			}
		});

		addModifyNameActionListener(new ModifyNameActionListener() {
			@Override
			public void nameModified(int index, String oldName, String newName) {
				refreshCurrentUpdatePane();
			}
		});
	}

	public Map<Class<Parameter>, NameableCreator<Parameter>> createNameableCreators() {
		Map<Class<Parameter>, NameableCreator<Parameter>> result = new HashMap<Class<Parameter>, NameableCreator<Parameter>>();

		result.put(Parameter.class, new AbstractNameableCreator<Parameter>() {
			@Override
			public Parameter createNameable() {
				return new Parameter("p");
			}

			@Override
			public String menuName() {
				return "参数名称";
			}

			@Override
			public EditorPane<Parameter> getUpdatePane() {
				return new ParameterPane();
			}
		});

		return result;
	}

	/**
	 * 在对话框中显示
	 * 
	 * @param owner
	 *            对话框父容器
	 * @return 返回值
	 */
	public int showInDialog(Component owner) {
		Component c = owner;

		if (c == null) {
			c = this;
		}

		return this.showDialog(SwingUtilities.getWindowAncestor(c), "参数编辑", 600, 400);
	}
}